﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YJKExcelFormTool.Forms
{
	public class TableHeadColumnMappingStorage
	{
		public static bool ReadColumnMappingInfo(ref HeadColumnMappingScheme scheme)
		{
			string settingFilePath = TableHeadColumnMappingStorage.GetSettingFilePath();
			if (!File.Exists(settingFilePath))
			{
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(settingFilePath);
			}
			catch (Exception ex)
			{
				ex.ToString();
				return false;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Root");
			return xmlNode != null && TableHeadColumnMappingStorage.ReadScheme(xmlNode, ref scheme);
		}

		public static bool WriteColumnMappingInfo(HeadColumnMappingScheme scheme)
		{
			string settingFilePath = TableHeadColumnMappingStorage.GetSettingFilePath();
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement xmlElement;
			if (!File.Exists(settingFilePath))
			{
				XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
				XmlNode xmlNode2 = xmlNode;
				xmlNode2.InnerText += " encoding=\"utf-8\"";
				xmlDocument.AppendChild(xmlNode);
				xmlElement = xmlDocument.CreateElement("", "Root", "");
				xmlDocument.AppendChild(xmlElement);
			}
			else
			{
				try
				{
					xmlDocument.Load(settingFilePath);
				}
				catch (Exception ex)
				{
					ex.ToString();
					return false;
				}
				xmlElement = (xmlDocument.SelectSingleNode("Root") as XmlElement);
				if (xmlElement == null)
				{
					return false;
				}
			}
			TableHeadColumnMappingStorage.WriteScheme(xmlDocument, xmlElement, scheme);
			bool result;
			try
			{
				xmlDocument.Save(settingFilePath);
				result = true;
			}
			catch (Exception ex2)
			{
				string message = ex2.Message;
				result = false;
			}
			return result;
		}

		private static bool ReadScheme(XmlNode rootNode, ref HeadColumnMappingScheme scheme)
		{
			XmlNode xmlNode = TableHeadColumnMappingStorage.FindFileNode(rootNode, scheme.SourceName);
			if (xmlNode == null)
			{
				return false;
			}
			string xpath = "SubScheme" + scheme.SheetIndex.ToString();
			XmlNode xmlNode2 = xmlNode.SelectSingleNode(xpath);
			if (xmlNode2 == null)
			{
				return false;
			}
			foreach (object obj in xmlNode2.ChildNodes)
			{
				XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
				try
				{
					string attribute = xmlElement.GetAttribute("Key");
					string attribute2 = xmlElement.GetAttribute("Value");
					scheme.AddMappingItem(new KeyValuePair<string, string>(attribute, attribute2));
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
			}
			return true;
		}

		private static void WriteScheme(XmlDocument xmlDoc, XmlNode rootNode, HeadColumnMappingScheme scheme)
		{
			XmlNode xmlNode = TableHeadColumnMappingStorage.FindFileNode(rootNode, scheme.SourceName);
			if (xmlNode != null)
			{
				rootNode.RemoveChild(xmlNode);
			}
			xmlNode = xmlDoc.CreateElement("", "SchemeName", "");
			((XmlElement)xmlNode).SetAttribute("fillPath", scheme.SourceName);
			rootNode.AppendChild(xmlNode);
			XmlNode xmlNode2 = xmlDoc.CreateElement("", "SubScheme" + scheme.SheetIndex.ToString(), "");
			xmlNode.AppendChild(xmlNode2);
			foreach (KeyValuePair<string, string> keyValuePair in scheme.MappingInfo)
			{
				XmlElement xmlElement = xmlDoc.CreateElement("Item");
				xmlElement.SetAttribute("Key", keyValuePair.Key);
				xmlElement.SetAttribute("Value", keyValuePair.Value);
				xmlNode2.AppendChild(xmlElement);
			}
		}

		private static XmlNode FindFileNode(XmlNode parentNode, string sourceName)
		{
			foreach (object obj in parentNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				XmlElement xmlElement = xmlNode as XmlElement;
				try
				{
					if (xmlElement.GetAttribute("fillPath").CompareTo(sourceName) == 0)
					{
						return xmlNode;
					}
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
			}
			return null;
		}

		private static string GetSettingFilePath()
		{
			return Product.WorkingLocation + "TableHeadColumnMappingInfo.xml";
		}

		private static string ReplaceInvalidFileNameChars(string fillName)
		{
			return fillName;
		}
	}
}
